/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.montecristo.posmobile.frameAux;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.text.InputType;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import org.montecristo.posmobile.R;


import java.util.ArrayList;
import java.util.List;

import org.montecristo.posmobile.previo.base.ManagerDB;
import org.montecristo.posmobile.previo.clases.encuesta.Applicacion;
import org.montecristo.posmobile.previo.clases.encuesta.Opcion;
import org.montecristo.posmobile.previo.clases.encuesta.Pregunta;




public class HeadlinesFragment extends ListFragment implements OnItemClickListener {
   	private ManagerDB manager;
	private Applicacion app;
	private String seccion;
	List<Pregunta> mHeadlinesList = new ArrayList<Pregunta>();
    ArrayAdapter<Pregunta> mListAdapter;
    OnHeadlineSelectedListener mHeadlineSelectedListener = null;

    public interface OnHeadlineSelectedListener {

        public void onHeadlineSelected(int index);
    }


    public HeadlinesFragment() {
        super();
    }

   @Override
    public void onStart() {
        super.onStart();
       
        getListView().setOnItemClickListener(this);
        loadCategory(0);
    }

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        manager = new ManagerDB(this.getActivity());
        String id=getActivity().getIntent().getStringExtra("idApp");
        seccion=getActivity().getIntent().getStringExtra("idSecc");
        app= manager.getApplicacionById(id);
    }

    /**
     * Sets the listener that should be notified of headline selection events.
     *@param listener the listener to notify.
     */
    public void setOnHeadlineSelectedListener(OnHeadlineSelectedListener listener) {
        mHeadlineSelectedListener = listener;
    }

    /**
     * Load and display the headlines for the given news category.
     *@param categoryIndex the index of the news category to display.
     */
    public void loadCategory(int categoryIndex) {
       mHeadlinesList=manager.getPreguntasBySeccion(seccion,app.getIdAplicacion());
    	mListAdapter = new ArrayAdapter<Pregunta>(getActivity(), R.layout.headline_item,mHeadlinesList){
    		@Override
     		public View getView(int position, View convertView,	android.view.ViewGroup parent) {
				View view = super.getView(position, convertView, parent);
				Pregunta entry = mHeadlinesList.get(position);
				TextView text1 = (TextView) view.findViewById(R.id.txt1);
				String text = (position+1) +" - " + ((Integer.parseInt(entry.getTipoPregunta()) >= 4)?entry.getSubseccionInfo():entry.getTexto())+" ";
				if(Integer.parseInt(entry.getTipoPregunta()) >= 4){
					text += entry.getTexto();
				}else if(entry.getTextoAuxiliar()!=null&&!entry.getTextoAuxiliar().isEmpty()){
					text += entry.getTextoAuxiliar();
				}
     			text1.setText(text);
     			text1.setTextColor(Color.BLUE); 
				if(entry.getRequerida().equals("1")){
					text1.setTextColor(Color.RED);    						
				}
				if(entry.getRespuesta()!=null){
					text1.setTextColor(Color.GREEN);
				}else{
					if(entry.getOpciones()!=null){
   						for (Opcion opc : entry.getOpciones()) {
   							if(opc.getRespuesta()!=null)
      								text1.setTextColor(Color.GREEN);           	     						
						}           						
   					}     						
				}     					
				return view;
     		}
     	};
    	setListAdapter(mListAdapter);
    }


   @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (null != mHeadlineSelectedListener) {
            mHeadlineSelectedListener.onHeadlineSelected(position);
        }
    }


    public void setSelectable(boolean selectable) {
        if (selectable) {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
        else {
            getListView().setChoiceMode(ListView.CHOICE_MODE_NONE);
        }
    }
}
